package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import cn.itcast.crm.domain.Customer;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.util.base.BaseAction;

import javax.ws.rs.core.MediaType;

@Controller
@ParentPackage("struts-default")
@Namespace("/")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea>{
	@Autowired
	private FixedAreaService fixedAreaService;

	@Action(value = "fixedArea_save",results = {@Result(name = "success",
		type = "redirect",location = "/pages/base/fixed_area.html")})
	public String saveOrUpdateFixedArea(){
		fixedAreaService.saveOrUpdate(model);
		return  SUCCESS;
	}
	//分页条件chaxun 
	@Action(value="fixedArea_pageQuery")
	public String pageQuery() throws IOException{
		Pageable pageable = new PageRequest(page -1, rows);
		System.out.println(page);
		System.out.println(rows);
		Page<FixedArea> pageData = fixedAreaService.pageQuery(pageable,model);
		Map<String,Object> map = new HashMap();
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		String json = JSONArray.toJSONString(map);
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);
		return NONE;
	}
	public String ids;
	
	@Action(value="fixedArea_delete" ,
			results={@Result(name = "success",type="redirect",location="/pages/base/fixed_area.html")})
	public String deleteFixedArea(){
		fixedAreaService.deleteFixedAreaById(ids);
		return SUCCESS;
	}
	//查询未关联定区
	@Action(value = "fixedArea_FindNoAssociationCustomer")
	public String findNoAssociationCustomer() throws IOException {
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationCustomer").
				accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
				String json = JSONArray.toJSONString(collection);
				ServletActionContext.getResponse().getWriter().write(json);
		return NONE;
	}
	@Action(value = "fixedArea_hsaAssociationCustomer")
	public String hsaAssociationCustomer() throws IOException {
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/" +  model.getId()).
				accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		String json = JSONArray.toJSONString(collection);
		ServletActionContext.getResponse().getWriter().write(json);
		return NONE;
	}
	//客户关联定区
	private String[] customerIds;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	@Action(value="fixedArea_associationCustomerToFixedArea",
			results={@Result(name = "success",type="redirect",location="/pages/base/fixed_area.html")})
	public String associationCustomerToFixedArea(){
		String customerStrId = StringUtils.join(customerIds, ",");
		WebClient.create("http://localhost:9002/crm_management/services/customerService/associationcustomertofixedarea?customerIdStr="+customerStrId +"&fixedAreaId="+model.getId()).put(null);
		return SUCCESS;
	}


	//快递员关联定区

	public Integer takeTimeId;
	public Integer courierId;
	@Action(value = "fixedArea_associationCourierToFixedArea",
			results = {@Result(name = "success",
			type = "redirect" ,location = "/pages/base/fixed_area.html")})
	public String associationCourierToFixedArea(){
			fixedAreaService.associationCourierToFixedArea(model,takeTimeId,courierId);
		return  SUCCESS;
	}
}
